
class Counter {
    public int count = 0;
    private Object locker = new Object();

    public void increaseCount1() {
        synchronized (locker) {
            ++count;
        }
    }

    public void increaseCount2() {
        synchronized (locker) {
            ++count;
        }
    }
}

public class Demo12 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread thread1 = new Thread(() -> {
            for (int i = 0; i < 50000; ++i) {
                counter.increaseCount1();
            }
        });

        Thread thread2 = new Thread(() -> {
            for (int i = 0; i < 50000; ++i) {
                counter.increaseCount2();
            }
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println("总数: " + counter.count);
    }
}
